package com.smartian.v2.impl;

import android.text.TextUtils;

import com.smartian.v2.AbstractTextParser;
import com.smartian.v2.OpLog;
import com.smartian.v2.TextPattern;
import com.smartian.v2.filters.TextParserInterceptorManager;
import com.smartian.v2.model.DateTimeNode;
import com.smartian.v2.model.ScheduleTaskBean;
import com.smartian.v2.utils.DateTimeHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * Created by tianweitong on 2020/3/14.
 */

public class EveryWorkDayTextParser extends AbstractTextParser {

    final List<DateTimeNode> mDateTimeNodeList  = new ArrayList<>();
    private int mFrequent = 1;
    private volatile  boolean mHasParsed = false;

    public EveryWorkDayTextParser(String sourceText, String repeatText, int repeatTextIndex) {
        super(sourceText, repeatText, repeatTextIndex);
    }


    @Override
    public ScheduleTaskBean getScheduleTaskBean() {
        startHandler();
        OpLog.d("TextParser",mDateTimeNodeList+" , "+mFrequent);
        if(mDateTimeNodeList!=null) {
            Collections.sort(mDateTimeNodeList);
        }

        DateTimeNode timeNode = DateTimeHelper.findBestDateNode(mDateTimeNodeList,DateTimeNode.TYPE_TIME);

        if( timeNode==null){
            return null;
        }
        if( TextUtils.isEmpty(timeNode.resultText)){
            return null;
        }

        List<DateTimeNode> dateTimeNodes  = new ArrayList<>();

        dateTimeNodes.add(timeNode);

        String time = timeNode.resultText;

        String dateTime = DateTimeHelper.formatTime(time);

        ScheduleTaskBean scheduleTaskBean = new ScheduleTaskBean();
        scheduleTaskBean.setFraquent(mFrequent);
        scheduleTaskBean.setRepeatType(TextPattern.REPEAT_EVERY_WORK_DAY);
        scheduleTaskBean.setRepeatText(getRepeatText());
        scheduleTaskBean.setDateTime(dateTime);
        scheduleTaskBean.setDateTimeFormat(getDateTimeFormat());
        scheduleTaskBean.setSourceText(getSourceText());
        scheduleTaskBean.setDateTimeNodes(dateTimeNodes);

        return  scheduleTaskBean;
    }

    private List<DateTimeNode> startParse() {

        mFrequent = parseFrequent();
        if(mFrequent<0) return null;

        List<DateTimeNode> dateTimeNodes = getParseTime();
        String repeatText = getRepeatText();

        if(repeatText.endsWith(TextPattern.WORD_NIGHT)) {
            adjustNightTime(dateTimeNodes, repeatText);
            return dateTimeNodes;
        }

        if(dateTimeNodes.size()>0) return dateTimeNodes;

        String currentTime = DateTimeHelper.getCurrentTime();

        DateTimeNode dateTimeNode = new DateTimeNode();
        dateTimeNode.text = "";
        dateTimeNode.index = 0;
        dateTimeNode.resultText = currentTime;
        dateTimeNode.locatorText = "";
        dateTimeNode.type = DateTimeNode.TYPE_TIME;

        dateTimeNodes.add(dateTimeNode);

        return dateTimeNodes;
    }

    private void adjustNightTime(List<DateTimeNode> dateTimeNodes, String repeatText) {
        Calendar calendar = Calendar.getInstance();

        for (int i = 0; i < dateTimeNodes.size(); i++) {
            DateTimeNode dateTimeNode = dateTimeNodes.get(i);
            String resultText = dateTimeNode.resultText;
            String hour = DateTimeHelper.readPrevNumberAtFlag(resultText,":");
            String min = DateTimeHelper.readNextNumberAtFlag(resultText,":");

            if(!TextUtils.isEmpty(hour)){
                int integerValue = DateTimeHelper.getIntegerValue(hour);
                if(integerValue<=12){
                    integerValue += 12;
                }
                if(integerValue==24){
                    integerValue = 0;
                }
                hour = String.valueOf(integerValue);
            }

            if(TextUtils.isEmpty(hour)){
                hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
                min = String.valueOf(calendar.get(Calendar.MINUTE));;
            }
            dateTimeNode.resultText  = String.format("%s:%s",hour,min);
            dateTimeNode.locatorText = repeatText;

        }

        if(dateTimeNodes.size()==0){

            DateTimeNode dateTimeNode = new DateTimeNode();
            dateTimeNode.text = getRepeatText();
            dateTimeNode.index = getRepeatTextIndex();
            dateTimeNode.resultText = "20:00";
            dateTimeNode.locatorText = repeatText;

            dateTimeNodes.add(dateTimeNode);

        }
    }

    @Override
    public int parseFrequent() {
        String repeatText = getRepeatText();
        if(repeatText.contains(TextPattern.DAY_15_MONTH)){
            return -1;
        }
        return super.parseFrequent();
    }

    private void startHandler() {
        if(!mHasParsed) {
            List<DateTimeNode> dateTimeNodes = startParse();
            if (dateTimeNodes != null) {
                mDateTimeNodeList.clear();
                dateTimeNodes = TextParserInterceptorManager.dispatchIntercept(this,TextPattern.REPEAT_EVERY_WORK_DAY,dateTimeNodes);
                mDateTimeNodeList.addAll(dateTimeNodes);
            }
            mHasParsed = true;
        }
    }

    @Override
    public boolean isValid() {
        startHandler();
        return mFrequent>0;
    }

    @Override
    public String getDateTimeFormat() {
        return "HH:mm";
    }
}
